//
//  WBRefreshView.m
//  sinaWeibo
//
//  Created by 李江波 on 2016/11/20.
//  Copyright © 2016年 lijiangbo. All rights reserved.
//

#import "WBRefreshView.h"
#import <Masonry.h>

@interface WBRefreshView ()

/**
 父视图
 */
@property(nonatomic ,weak) UIScrollView *scrollView;

@property(nonatomic ,weak) UILabel *rightLable;
@property(nonatomic ,weak) UIActivityIndicatorView *indeicator;
@property(nonatomic ,weak) UIImageView *leftImage;
@end

static CGFloat refreshH = 60;
@implementation WBRefreshView

-(instancetype)initWithFrame:(CGRect)frame{

    CGRect rect = CGRectMake(0, -refreshH, kUISCRRENW, refreshH);
    if (self = [super initWithFrame:rect]) {
        
        [self setupUI];
    }
    return self;
}

-(void)setupUI{

    self.backgroundColor = [UIColor cz_randomColor];
    
    UIImageView *bgImg = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"refreshbg"]];
    UIImageView *leftImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"tableview_pull_refresh"]];
    UILabel *rightLable = [UILabel cz_labelWithText:@"下拉刷新" fontSize:14 color:[UIColor orangeColor]];
    UIActivityIndicatorView *indeicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
    
    [self addSubview:bgImg];
    [self addSubview:leftImage];
    [self addSubview:rightLable];
    [self addSubview:indeicator];
    
    self.rightLable = rightLable;
    self.indeicator = indeicator;
    self.leftImage = leftImage;
    
    [bgImg mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.right.bottom.equalTo(self);
    }];
    
    [rightLable mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.equalTo(self).offset(10);
        make.centerY.equalTo(self);
    }];
    
    [leftImage mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.equalTo(rightLable.mas_left).offset(-5);
        make.centerY.equalTo(self);
        
    }];
    
    [indeicator mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(leftImage);
    }];
    
}

-(void)willMoveToSuperview:(UIView *)newSuperview{

    [super willMoveToSuperview:newSuperview];
    
    _scrollView = (UIScrollView *)newSuperview;
    //KVO
    [_scrollView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:nil];
    
    self.status = refreshNormal;
    
}
//监听kvo
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{

    CGFloat moveY = self.scrollView.contentOffset.y;
    CGFloat changeY = - (NAVHeight + refreshH);
    //判断状态
    if (self.scrollView.isDragging) {
        //正在拖拽
        if (self.status == already && moveY > changeY) {
            
            self.status = refreshNormal;
        }else if (self.status == refreshNormal && moveY < changeY) {
        
            self.status = already;
        
        }
    }else{
        
        if (self.status == already) {
            
            self.status = refreshing;
        }
        
    }
    
}


-(void)setStatus:(Status)status{

    _status = status;
    
    switch (status) {
        case refreshNormal:
        {
            self.leftImage.hidden = NO;
            [self.indeicator stopAnimating];
            self.rightLable.text = @"下拉刷新";
            [UIView animateWithDuration:0.25 animations:^{
                
                self.leftImage.transform = CGAffineTransformIdentity;
                
            }];
            
            self.scrollView.contentInset = UIEdgeInsetsMake(NAVHeight, 0, 0, 0);
        
            break;
        }
        case already:
        {
            self.rightLable.text = @"松手刷新";
            [UIView animateWithDuration:0.25 animations:^{
                
                self.leftImage.transform = CGAffineTransformMakeRotation(M_PI);
            }];
            break;
        }
        case refreshing:
        {
            self.leftImage.hidden = YES;
            [self.indeicator startAnimating];
            self.rightLable.text = @"正在刷新";
            
            [self sendActionsForControlEvents:UIControlEventValueChanged];
            //停留一段时间
            [UIView animateWithDuration:0.25 animations:^{
                self.scrollView.contentInset = UIEdgeInsetsMake(NAVHeight + refreshH, 0, 0, 0);
            }];
            
            break;
        }
      
    }

}


/**
 停止刷新
 */
-(void)stopRefreshing{

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        self.status = refreshNormal;
    });
    
}


//移除kvo
-(void)dealloc{

    [self.scrollView removeObserver:self forKeyPath:@"contentOffset"];
    
}


@end
